#include <gtk_api.h>
#include <core.h>

/* API manual */
/* http://library.gnome.org/devel/gtk/stable/api-index-full.html */
/* http://library.gnome.org/devel/gobject/unstable/gobject-Type-Information.html */

/* 
 * Some structs we may use:
 * GtkTextView.
 * GtkTextBuffer.
 * GtkScrollWindow
 * GtkCombo
 * GtkEntry
 * GtkListStore
 * GtkWindow
 * others that I do not know what's it.
 */

/* TODO: modify struct */
GtkWidget *G_view = NULL;
GtkEntry *G_eSearch;

static void toolwin_create(GtkWidget *vbox);
static void textwin_create(GtkWidget *vbox);
static void topwin_create(GtkWidget *vbox);
static void midwin_create(GtkWidget *vbox);
static void bottomwin_create(GtkWidget *vbox);

static void add_widget_to_window(GtkWidget *window, GtkWidget *widget);

/* core */
static GtkWidget *build_window(void);
static GtkWidget *build_main_vbox(void);

/* callback */
static void input_words(GtkEntry *entry, void *oTopWin);
static void empty_foo(GtkEntry *entry, void *oTopWin);

/* others api */
static void unfocus_combo_arrow(GtkWidget *widget, gpointer data);

static void unfocus_combo_arrow(GtkWidget *widget, gpointer data)
{
	if (!GTK_IS_ENTRY(widget))
		GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_FOCUS);
}

void add_to_list(GtkTreeView *list, const gchar *str)
{
	GtkListStore *store;
	GtkTreeIter iter;
	
	store = GTK_LIST_STORE(gtk_tree_view_get_model(list));
	
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(store, &iter, 0, str, -1);
}

GtkWidget *ListWin_Create(void)
{
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GtkTreeSelection *selection;
	GtkWidget *scrolledwindow;
	GtkListStore *list_model;
	GtkTreeView *treeview_;

	list_model = gtk_list_store_new (1, G_TYPE_STRING);
	treeview_ = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_model)));

	gtk_widget_show(GTK_WIDGET(treeview_));
	gtk_tree_view_set_rules_hint(treeview_, TRUE);
	gtk_tree_view_set_headers_visible(treeview_, FALSE);

	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes("word", renderer,
						     "text", 0, NULL);
	gtk_tree_view_append_column(treeview_, column);

	selection = gtk_tree_view_get_selection(treeview_);

	scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_show(scrolledwindow);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW (scrolledwindow),
				       GTK_SHADOW_ETCHED_IN);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);

	gtk_container_add(GTK_CONTAINER(scrolledwindow), GTK_WIDGET(treeview_));

	/* add words into word_list */
	get_all_words_in_dict((void *)treeview_, NULL);
	return scrolledwindow;
}

bool indexwin_create(GtkWidget *hpaned)
{
	GtkWidget *scrolledwindow = ListWin_Create();
	gtk_paned_pack1(GTK_PANED(hpaned), scrolledwindow, TRUE, FALSE);

//	ResultWin_Create(notebook);

	return 1;
}

static void toolwin_create(GtkWidget *vbox)
{
	GtkWidget *hbox;
	hbox = gtk_hbox_new(0, 0);
	gtk_widget_show(hbox);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, 0 , 0, 3);
}

static void textwin_create(GtkWidget *vbox)
{
	GtkWidget *hbSearchPanel;
	GtkButton *btClose, *btFind;
	GtkWidget *scrolledWindow;
	GtkWidget *image;

	image = gtk_image_new_from_stock(GTK_STOCK_CANCEL,
				GTK_ICON_SIZE_SMALL_TOOLBAR);
	hbSearchPanel = gtk_hbox_new(FALSE, 0);
	btClose = GTK_BUTTON(gtk_button_new());
	gtk_widget_show(image);

	gtk_container_add(GTK_CONTAINER(btClose), image);
	gtk_button_set_relief(btClose, GTK_RELIEF_NONE);
	GTK_WIDGET_UNSET_FLAGS(btClose, GTK_CAN_FOCUS);
	g_signal_connect(G_OBJECT(btClose), "clicked",
		G_CALLBACK(empty_foo), NULL);
#if 1
	gtk_box_pack_start(GTK_BOX(hbSearchPanel), GTK_WIDGET(btClose), FALSE, FALSE, 3);
	G_eSearch = GTK_ENTRY(gtk_entry_new());
	gtk_widget_set_size_request(GTK_WIDGET(G_eSearch), 20, -1);
	g_signal_connect(G_OBJECT(G_eSearch), "key_press_event",
		G_CALLBACK(empty_foo), NULL);
	
	gtk_box_pack_start(GTK_BOX(hbSearchPanel), GTK_WIDGET(G_eSearch), TRUE, TRUE, 0);
	btFind = GTK_BUTTON(gtk_button_new_from_stock("gtk-find"));
	gtk_button_set_relief(btFind, GTK_RELIEF_NONE);
	GTK_WIDGET_UNSET_FLAGS(btFind, GTK_CAN_FOCUS);
	g_signal_connect(G_OBJECT(btFind), "clicked",
		G_CALLBACK(empty_foo), NULL);
	
	gtk_box_pack_start(GTK_BOX(hbSearchPanel), GTK_WIDGET(btFind), FALSE, FALSE, 5);
	
	gtk_widget_show_all(hbSearchPanel);
	gtk_box_pack_start(GTK_BOX(vbox), hbSearchPanel, FALSE, FALSE, 0);
#endif

	scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
	G_view = gtk_text_view_new();
	gtk_widget_set_size_request (G_view, 250, 150); 
	gtk_text_view_set_editable (GTK_TEXT_VIEW (G_view), FALSE); 
	gtk_container_add(GTK_CONTAINER(scrolledWindow), G_view);
	gtk_widget_show_all(scrolledWindow);
	gtk_box_pack_start(GTK_BOX(vbox), scrolledWindow, FALSE, FALSE, 0);
}

static void input_words(GtkEntry *entry, void *oTopWin)
{
	const gchar *sWord = gtk_entry_get_text(entry);
	char buf[1024];
	GtkTextBuffer *t_buf;
	int len;

	memset(buf, 0, 1024);

	len = get_word_explain(sWord, buf);
	if (buf[0] != '\0') {
		//gtk_entry_set_text(G_eSearch, buf);
		t_buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(G_view));
		gtk_text_buffer_set_text(t_buf, buf, -1);
	} else 
		gtk_entry_set_text(G_eSearch, "");
}

static void empty_foo(GtkEntry *entry, void *oTopWin)
{
	return ;
}

/* Widget Sequence:
 * clear_button,
 * WordCombo
 */
static void topwin_create(GtkWidget *vbox)
{
	GtkWidget *hbox = gtk_hbox_new(FALSE, FALSE);
	GtkWidget *button, *WordCombo;
	GtkListStore *list_store;

	gtk_widget_show(hbox);
	gtk_box_pack_start(GTK_BOX(vbox),hbox,0,0,3);
	
	/* first - clear */
	button = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button),gtk_image_new_from_stock(GTK_STOCK_UNDO,GTK_ICON_SIZE_BUTTON));
	gtk_widget_show_all(button);
	gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
	GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS);
	gtk_box_pack_start(GTK_BOX(hbox), button, 0, 0, 0);

	/* second - input and .. */
	list_store = gtk_list_store_new(1, G_TYPE_STRING);
	WordCombo = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(list_store), 0);
	g_object_unref(G_OBJECT(list_store));
	gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(WordCombo), FALSE);
	gtk_container_forall(GTK_CONTAINER(WordCombo), unfocus_combo_arrow, NULL);
	gtk_widget_set_size_request(WordCombo, 60, -1);
	gtk_widget_show(WordCombo);
	gtk_entry_set_max_length(GTK_ENTRY(GTK_BIN(WordCombo)->child), 255);
	g_signal_connect(G_OBJECT(GTK_BIN(WordCombo)->child), "changed",
			  G_CALLBACK(input_words), NULL);
	gtk_box_pack_start(GTK_BOX(hbox), WordCombo, TRUE, TRUE, 3);

	/* third */
	button = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button),gtk_image_new_from_stock(GTK_STOCK_HOME,GTK_ICON_SIZE_BUTTON));
	gtk_widget_show_all(button);
	gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
	GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS);
	gtk_box_pack_start(GTK_BOX(hbox),button,0,0,0);
}

static void midwin_create(GtkWidget *vbox)
{
	GtkWidget *vbox1, *hbox, *hpaned;

	hbox = gtk_hbox_new(FALSE, 2);

	hpaned = gtk_hpaned_new();
	gtk_widget_show(hpaned);
	gtk_box_pack_start(GTK_BOX(vbox), hpaned, TRUE, TRUE, 0);

	/**/
	indexwin_create(hpaned);
	
	vbox1 = gtk_vbox_new(FALSE, 0);
	gtk_widget_show(vbox1);
	textwin_create(vbox1);
	gtk_paned_pack2(GTK_PANED(hpaned), vbox1, TRUE, FALSE);
}

static void bottomwin_create(GtkWidget *vbox)
{
	topwin_create(vbox);
}

static GtkWidget *build_window(void)
{
	GtkWidget *window = NULL;

	/* FIXME: here need check SUCCESS? */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), soft_prog);
	gtk_window_set_default_size(GTK_WINDOW(window), 
		GRAH_DEFAULT_WIN_WIDTH, GRAH_DEFAULT_WIN_HEIGTH);

	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

	return window;
}

static void add_widget_to_window(GtkWidget *window, GtkWidget *widget)
{
	gtk_container_add(GTK_CONTAINER(window), widget);
}

/* Our main vbox has 3 parts: Top / Mid / Bottom. 
 * main vbox is belong to main window.
 */
static GtkWidget *build_main_vbox(void)
{
	GtkWidget *box;
	box = gtk_vbox_new(FALSE,0);
	topwin_create(box);
	midwin_create(box);
	bottomwin_create(box);
	return box;
}

/* 
 * Build a core window with default setting the vbox. 
 *
 * Many windows are copied from Stardict. But not of all.
 * In fact I want to do that, but hated C++ syntax.
 * So just use what I know.
 *
 * Who will complete this hard work? Make ourself interface.
 */
GtkWidget *build_main_window(void)
{
	GtkWidget *vbox, *window;

	vbox = build_main_vbox();
	window = build_window();
	assert(window && vbox);
	add_widget_to_window(window, vbox);

	return window;
}
